Page ({

    data: {
        isShow: false,
        timer: '会员未开通',
        userID: null
    },

    onLoad (query) {
        console.log(query);
        if (query.data == "会员已过期" || query.data == "会员未开通") {
            this.setData({
                isShow: true,
                timer: query.data
            })
        } else {
            this.setData({  
                timer: query.data
            })
        }   
    },

    getPrice () {
        let that = this;
        let orderNum = 'ssssss' + getApp().globalData.userID;
        wx.getStorage({
            key: 'userID',
            success(res) {
                that.setData({
                    userID: res.data
                }) 
            }
        });
        let userId = this.data.userID;
        if (userId == null) {
            userId = getApp().globalData.userID
        }
        wx.request({
            url: 'https://localhost:80/wcpay/pay',
            data: {
                id: userId,
                name: '测试商品',
                // 订单编号
                orderNum: orderNum,
                // orderNum: '13213214645',
                price: 6.6,
                code: getApp().globalData.code
            },
            header: {
               'content-type': 'application/json' //默认值
            },
            method: 'post',
            success: function (res) {
                console.log(res.data);
                // 发起支付
                console.log(res.data.package);
                let pack = res.data.package;
                let arr = [];
                arr = pack.split('=');
                console.log(arr[1]);
                if (arr[1] == 'null') {
                    wx.showModal({
                        title: '温情提示',
                        content: '亲，可能是因为网络问题，请稍后充值',
                        success(res) {
                            if (res.confirm) {
                                console.log('用户点击确定')
                            } else if (res.cancel) {
                                console.log('用户点击取消')
                            }
                        }
                    })
                } else {
                    wx.requestPayment({
                        timeStamp: res.data.timeStamp,
                        nonceStr: res.data.nonceStr,
                        package: res.data.package,
                        signType: 'MD5',
                        paySign: res.data.paySign,
                        success (res) {
                            console.log('--------------------------');
                            console.log(res);
                            wx.request({
                                url: 'https://localhost:80/admins/query',
                                data: {
                                    adminId: getApp().globalData.userID
                                },
                                header: {
                                    'content-type': 'application/json'
                                },
                                method: 'post', 
                                success (res) { 
                                    console.log(res.data);
                                    that.setData({ 
                                        isShow: false,
                                        timer: res.data.adminDate
                                    }) 
                                    console.log(that.data.timer);
                                    // 将用户身份状态改变
                                    getApp().globalData.vipState = res.data.adminRole;
                                },
                                fail () {
                                    console.log('测试one');
                                }
                            });
                            wx.showModal({
                                title: '友情提示',
                                content: '充值成功',
                                text:'center',
                                complete() {}
                            });
                            console.log('--------------------------');
                        },
                        fail (res) {
                            console.log('测试two');
                            wx.showModal({
                                title: '温情提示',
                                content: '亲，可能是因为网络问题，请稍后充值',
                                success(res) {
                                    if (res.confirm) {
                                        console.log('用户点击确定')
                                    } else if (res.cancel) {
                                        console.log('用户点击取消')
                                    }
                                }
                            })
                        }
                    })
                }       
            }
        });
    },
  
    exploit () {
        this.getPrice();
    },

    toPay () {
        this.getPrice();
    },
})


